package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author zzw
 * @date 2025/5/6
 * @description 店铺管理
 */
@Api(tags = "店铺管理接口")
@Slf4j
@RestController
@RequestMapping("/admin/shop")
public class ShopController {

    // 店铺状态的key
    private static final String SHOP_STATUS_KEY = "SHOP_STATUS";

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 获取店铺营业状态
     *
     * @return Result<Integer>
     */
    @ApiOperation("获取店铺营业状态")
    @GetMapping("/status")
    public Result<Integer> getStatus() {
        Integer status = (Integer) redisTemplate.opsForValue().get(SHOP_STATUS_KEY);
        log.info("获取店铺营业状态:{}", status);
        return Result.success(status);
    }

    /**
     * 修改店铺营业状态
     *
     * @param status 状态
     * @return Result<String>
     */
    @ApiOperation("修改店铺营业状态")
    @PutMapping("/{status}")
    public Result<String> setStatus(
            @ApiParam(value = "营业状态(1:营业中 0:打烊中)", required = true) @PathVariable Integer status) {
        log.info("修改营业状态为：{}", Objects.equals(status, StatusConstant.ENABLE) ? "营业中" : "打烊中");
        redisTemplate.opsForValue().set(SHOP_STATUS_KEY, status);
        return Result.success();
    }
}
